<template>
	<view style="height:100vh;background: #fff;">
		<view class="img-a">
			<view class="t-b">
				您好，欢迎使用
				<br />
				毫米波雷达心跳呼吸检测小程序
			</view>
		</view>
		<view class="login-view" style="">
			<view class="t-login">
				<form class="cl">
					<view class="t-a">
						<text class="txt">手机号</text>
						<input type="number" name="phone" placeholder="请输入您的手机号" maxlength="11" v-model="phone" />
					</view>
					<view class="t-a">
						<text class="txt">密码</text>
						<input type="password" name="code" maxlength="18" placeholder="请输入您的密码" v-model="password" />
					</view>
					<button @tap="login1()">登 录</button>
					<!-- <view class="reg" @tap="reg()">注 册</view> -->
				</form>
				<!-- <view class="t-f"><text>—————— 第三方账号登录 ——————</text></view>
				<view class="t-e cl">
					<view class="t-g" @tap="wxLogin()"><image src="https://zhoukaiwen.com/img/loginImg/wx.png"></image></view>
					<view class="t-g" @tap="zfbLogin()"><image src="https://zhoukaiwen.com/img/loginImg/qq.png"></image></view>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script lang="ts">
import { ref,defineComponent,reactive } from 'vue'
import { useLoginService } from "@/store/login.service"
import type { LoginParam, UserInfo } from '../../api/types';
import { Md5 } from 'ts-md5';
import { login } from '../../api/login/login';
import jwt_decode from 'jwt-decode'
import { useAuthStore } from '../../store/user';
export default defineComponent({
  setup() {
    const userStore = useAuthStore()
    // 获取登录服务
    const loginService = useLoginService()
    const param = reactive({
        username:"admin",
        password:"public",
    })
    // 定义数据
    const phone = ref('')
    const password = ref('')

    // 定义方法
    const login1 = async () => {
      // 验证手机号和密码是否为空
      if (phone.value === '' || password.value === '') {
        uni.showToast({
          title: '手机号或密码不能为空',
          icon: 'none'
        })
        return
      }
      const loginParam:LoginParam= {
        username:param.username,
        password:Md5.hashStr(param.password)
    }
      // 调用登录服务的方法
      // const result = await loginService.login(phone.value, password.value)
      const res = await login(loginParam);
      // 判断登录结果是否成功
      console.log(res)
      if (res.code===200) {
        // localStorage.setItem('token',res.data.token);
        uni.setStorageSync('token', res.data.token);
        const decode = jwt_decode(res.data.token)
        userStore.setAuth(true);
        userStore.setUser(decode);
        uni.showToast({
          title: '登录成功',
          icon: 'success'
        })
        // 跳转到主页面或其他页面，根据您的需求修改url参数
        uni.switchTab({
          url: '/pages/index/index'
        })
      } else {
        // 登录失败，显示错误信息
        uni.showToast({
          title: res.msg,
          icon: 'none'
        })
        uni.switchTab({
          url: '/pages/index/index'
        })
      }
    }

    // 返回数据和方法
    return {
      phone,
      password,
      login1
    }
  }
})
</script>

<style>
.txt {
	font-size: 32rpx;
	font-weight: bold;
	color: #333333;
}
.img-a {
	width: 100%;
	height: 450rpx;
	background-image: url(https://zhoukaiwen.com/img/loginImg/head.png);
	background-size: 100%;
}
.reg {
	font-size: 28rpx;
	color: #fff;
	height: 90rpx;
	line-height: 90rpx;
	border-radius: 50rpx;
	font-weight: bold;
	background: #f5f6fa;
	color: #000000;
	text-align: center;
	margin-top: 30rpx;
}

.login-view {
	width: 100%;
	position: relative;
	margin-top: -120rpx;
	background-color: #ffffff;
	border-radius: 8% 8% 0% 0;
}

.t-login {
	width: 600rpx;
	margin: 0 auto;
	font-size: 28rpx;
	padding-top: 80rpx;
}

.t-login button {
	font-size: 28rpx;
	background: #2796f2;
	color: #fff;
	height: 90rpx;
	line-height: 90rpx;
	border-radius: 50rpx;
	font-weight: bold;
}

.t-login input {
	height: 90rpx;
	line-height: 90rpx;
	margin-bottom: 50rpx;
	border-bottom: 1px solid #e9e9e9;
	font-size: 28rpx;
}

.t-login .t-a {
	position: relative;
}

.t-b {
	text-align: left;
	font-size: 42rpx;
	color: #ffffff;
	padding: 130rpx 0 0 70rpx;
	font-weight: bold;
	line-height: 70rpx;
}

.t-login .t-c {
	position: absolute;
	right: 22rpx;
	top: 22rpx;
	background: #5677fc;
	color: #fff;
	font-size: 24rpx;
	border-radius: 50rpx;
	height: 50rpx;
	line-height: 50rpx;
	padding: 0 25rpx;
}

.t-login .t-d {
	text-align: center;
	color: #999;
	margin: 80rpx 0;
}

.t-login .t-e {
	text-align: center;
	width: 250rpx;
	margin: 80rpx auto 0;
}

.t-login .t-g {
	float: left;
	width: 50%;
}

.t-login .t-e image {
	width: 50rpx;
	height: 50rpx;
}

.t-login .t-f {
	text-align: center;
	margin: 150rpx 0 0 0;
	color: #666;
}

.t-login .t-f text {
	margin-left: 20rpx;
	color: #aaaaaa;
	font-size: 27rpx;
}

.t-login .uni-input-placeholder {
	color: #aeaeae;
}

.cl {
	zoom: 1;
}

.cl:after {
	clear: both;
	display: block;
	visibility: hidden;
	height: 0;
	content: '\20';
}
</style>
